const app = getApp()
const util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host:app.globalData.host,
    info:'',
  },

  infoInput:function(e){
    this.setData({
      info:e.detail.value
    });
  },
  condition:function(){
    this.getMsgList(1,'');
  },
  /**
   * 收藏/取消收藏
   * @param {*} e 
   */
  collect:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var collect = data.collect;
    util.request('collect/save', {related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].collect';
        if(collect.is_collect > 0){
          that.setData({
              [row]:{
                count:collect.count-1,
                is_collect:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:collect.count+1,
              is_collect:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },



  /**
   * 点赞/取消点赞
   * @param {*} e 
   */
  thumb:function(e){
    var index = e.currentTarget.dataset.index,data = this.data.pageList[index],that = this;
    var thumb = data.thumb;
    util.request('thumb/save', {type:2,related_id:data['id']}, function (data) {
      if(data.code == 1){
        var row = 'pageList['+index+'].thumb';
        if(thumb.is_thumb > 0){
          that.setData({
              [row]:{
                count:thumb.count-1,
                is_thumb:0,
              }
          })
        }else{
          that.setData({
            [row]:{
              count:thumb.count+1,
              is_thumb:1,
            }
        })
        }
      }else{
        util.fail(data.msg,that);
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    if(e.info){
      this.setData({
        info:e.info,
      })
    }
    //内容列表
    this.getMsgList(1,'');
  },
  onShow:function(){
    this.getMsgList(1,'');
  },

  onPullDownRefresh: function () {
    if (!this.loading) {
      wx.showNavigationBarLoading();
      this.getMsgList(1,'top');
    }
  },

  onReachBottom: function () {
    var that = this;
    if (!this.data.loading && this.data.page < this.data.pages) {
      that.setData({
        isHideLoadMore: false,
        page: this.data.page+1
      });
      this.getMsgList(that.data.page, 'bottom');
    }
  },

  getMsgList: function(pageNo,pos) {
    if(this.data.info !=''){
      var url = 'article/index',param = {type:[1,2],info:this.data.info};
      param.pageNo = pageNo;
      console.log(param);
      util.pageList(this,url,param, pos);
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

})